﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.Sys
{
    public class ServiceOperatorInfo : Benefit.Interface.Sys.IOperatorInfo
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorInfo(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 增加一个操作员信息，每个操作员只有一条操作员信息
        /// </summary>
        public void AddOperatorInfo(int operatorId)
        {
            Models.Sys.OperatorInfo info = new Models.Sys.OperatorInfo();
            info.AllTradeDays = 0;
            info.FirstTradeHistoryId = 1;
            info.LastLoginTime = DateTime.Now;
            info.LastTradeHistoryId = 1;
            info.OperatorId = operatorId;
            db.OperatorInfo.Add(info);
            db.SaveChanges();
        }
        /// <summary>
        /// 删除指定日期的结算记录时把交易天数减一
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void UnsetDayWorking(int tradeHistoryId)
        {
            var query = db.Operators.ToList();
            foreach (Models.Sys.Operator op in query)
            {
                Interface.View.IOperatorDayChange iodc = new View.ServiceOperatorDayChange(db);
                Models.View.OperatorDayChange change = iodc.GetOperatorDayChange(op.Id, tradeHistoryId);
                if (change.BillCount > 0)
                {
                    var _query = db.OperatorInfo.Where(a => a.OperatorId == op.Id).ToList();
                    _query.First().AllTradeDays = _query.First().AllTradeDays - 1;
                }
            }
            db.SaveChanges();
        }
        public void OperatorWorking(int tradeHistoryId)
        {
            var query = db.Operators.ToList();
            foreach (Models.Sys.Operator op in query)
            {
                Interface.View.IOperatorDayChange iodc = new View.ServiceOperatorDayChange(db);
                Models.View.OperatorDayChange change = iodc.GetOperatorDayChange(op.Id, tradeHistoryId);
                if (change.BillCount > 0)
                {
                    var _query = db.OperatorInfo.Where(a => a.OperatorId == op.Id).ToList();
                    if (_query.Count() > 0)//多次交易
                    {
                        Models.Sys.OperatorInfo info = _query.First();
                        _query.First().AllTradeDays = info.AllTradeDays + 1;
                        _query.First().LastTradeHistoryId = tradeHistoryId;
                        db.SaveChanges();
                    }
                    else//首次交易
                    {
                        Models.Sys.OperatorInfo info = new Models.Sys.OperatorInfo();
                        info.AllTradeDays = 1;
                        info.FirstTradeHistoryId = tradeHistoryId;
                        info.LastTradeHistoryId = tradeHistoryId;
                        info.LastLoginTime = DateTime.Now;
                        info.OperatorId = op.Id;
                        db.OperatorInfo.Add(info);
                        db.SaveChanges();
                    }
                }
            }
        }

        public Models.Sys.OperatorInfo GetOperatorInfo(int operatorid)
        {
            Models.Sys.OperatorInfo info = null;
            var query = db.OperatorInfo.Where(a => a.OperatorId == operatorid).ToList();
            if (query.Count() > 0)
            {
                info = query.First();
            }
            return info;
        }

        public List<Models.Sys.OperatorInfo> GetList()
        {
            return db.OperatorInfo.ToList();
        }
    }
}